package com.nlp.server.controller;

import com.nlp.common.core.web.controller.BaseController;
import com.nlp.common.core.web.pojo.AjaxResult;
import com.nlp.server.utils.SecurityUtils;
import com.nlp.server.utils.SseEmitterUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * sse控制层
 * @author shiliuyinzhen
 */
@RestController
@RequestMapping("/sse")
@Api(tags = "sse相关接口")
public class SseEmitterController extends BaseController {

    /**
     * 创建sse连接
     * @return
     */
    @ApiOperation("创建连接")
    @GetMapping(value = "/connect", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(){
//        return SseEmitterUtils.addSseEmitter(109L);
        return SseEmitterUtils.addSseEmitter(SecurityUtils.getUserId());
    }

    /**
     * 关闭sse连接
     * @return
     */
    @ApiOperation("关闭连接")
    @PostMapping("/close")
    public AjaxResult close(){
//        return toAjax(SseEmitterUtils.closeSseEmitter(109L));
        return toAjax(SseEmitterUtils.closeSseEmitter(SecurityUtils.getUserId()));
    }

}
